package editor
{
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class pi_rect_dev extends uif_component {
		private var rect: uif_component;
		private var r_size: Point;
		
		public function pi_rect_dev(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0) {
			var dw: Number = awidth / 2;
			var dh: Number = aheight / 2;
			super(parent, ax + dw, ay + dh, awidth, aheight);
			
			rect = new uif_component(this, -dw, -dh, awidth, aheight, 'rect');
			rect.addEventListener(MouseEvent.MOUSE_DOWN, doDown);
			rect.addEventListener(uif_component.RESIZE, function(e: Event): void {
				dispatchEvent(e);
			});
			
			init_controls();
		}
		
		public function init_controls(): void {
			var _roll: pi_roll = new pi_roll(this, x + width, y);
			var _resize: pi_resize = new pi_resize(this, x + width, y + height);
			
			function rePosControls(e: Event=null): void {
				_roll.x = rect.width / 2;
				_roll.y = -rect.height / 2;
				_resize.x = rect.width / 2;
				_resize.y = rect.height / 2;
			}
			
			this.addEventListener(uif_component.POSITION, rePosControls);
			this.addEventListener(uif_component.RESIZE, rePosControls);
			
			var pstart: Point = new Point();
			var roll: Number = rotation;
			var active: Boolean = false;
			
			function rotate(e: MouseEvent): void {
				var newPos: Point = app.mousePos;
				roll 		-= pstart.y - newPos.y;
				rotation 	= Math.round(roll / 5) * 5;
				pstart 		= newPos;
			}
			
			function scale(e: MouseEvent): void {
				var newSize: Point = new Point(rect.width, rect.height);
				var newPos: Point = app.mousePos;
				var pdelta: Point = newPos.subtract(pstart);
				var delta: Number = (pdelta.x + pdelta.y) / 2;
				newSize.x = newSize.x + delta; 
				newSize.y = newSize.y + delta;
				trace(newSize);
				
				if ((newSize.x > 10) && (newSize.y > 10)) {
					rect.resize(newSize.x, newSize.y);
					rect.x = -rect.width / 2; 
					rect.y = -rect.height / 2; 
					rePosControls();
				}
				pstart = newPos;
			}
			
			function removeMyEvent(e: MouseEvent): void {
				if (active) { 
					app.removeEventListener(MouseEvent.MOUSE_MOVE, rotate);
					app.removeEventListener(MouseEvent.MOUSE_MOVE, scale);
					r_size = rect.size;
					active = false;
				}
			}
			
			app.addEventListener(MouseEvent.ROLL_OUT, function(e: MouseEvent): void {
				removeMyEvent(e);
			});
			
			app.addEventListener(MouseEvent.CLICK, removeMyEvent);
			
			_roll.addEventListener(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				pstart 		= app.mousePos;
				roll		= rotation;
				active		= true;
				app.addEventListener(MouseEvent.MOUSE_MOVE, rotate);
			});
			_resize.addEventListener(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				pstart 		= app.mousePos;
				active		= true;
				app.addEventListener(MouseEvent.MOUSE_MOVE, scale);
			});
			rePosControls();
		}
		
		public function reset(a_size: Point): void {
			r_size = a_size;
			rect.x = -a_size.x / 2;
			rect.y = -a_size.y / 2;
			rotation = 0;
			resize(a_size.x, a_size.y);
			rect.resize(a_size.x, a_size.y);
		}
		
		protected function get photo(): pi_source {
			return (parent as pi_source);
		}
		
		public function getMatrix(): Matrix {
			var m:Matrix = new Matrix();
			var sl: Number = photo.sourceScale;
			
//			m.scale(sl, sl);
			m.translate((photo.source.x - x) / sl, (photo.source.y - y) / sl); // Трансляция к координатам фотографии

			m.rotate(rotation / 360 * Math.PI * 2);

			m.translate(rect.width / 2 / sl, rect.height / 2 / sl);
			m.scale(width / rect.width, width / rect.width);
			return m;
		}
		
		private function doDown(e: MouseEvent): void {
			beginDrag(5);	
		}		
	}
}